package com.example.administrator.smartdevice.net;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/13.
 */

public class HttpUtils {

    public static void get(Context context, final int requestCode, String url, HashMap<String, String> params, final RequestCallback requestCallback) {
        OkGo.<String>post(url)
                .tag(context)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        requestCallback.onSuccessCallback(requestCode, response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        requestCallback.onFailureCallback(requestCode, response.body());
                    }
                });
    }

    /**
     * 后台使用@requestbody时，表示接的参数是一个string化的json
     */
    public static void post(Context context, final int requestCode, String url, String jsonObject, final RequestCallback requestCallback) {
        OkGo.<String>post(url)
                .tag(context)
                .upJson(jsonObject)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        requestCallback.onSuccessCallback(requestCode, response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        requestCallback.onFailureCallback(requestCode, response.body());
                    }

                });
    }


    public static void post(Context context, final int requestCode, String url, Map<String, String> params, final RequestCallback requestCallback) {
        OkGo.<String>post(url)
                .tag(context)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        requestCallback.onSuccessCallback(requestCode, response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        requestCallback.onFailureCallback(requestCode, response.body());
                    }
                });
    }

    /**
     * 获取结果内容
     */
    public static Object getData(Object object, Class<?> clazz) {
        JSONObject response = JSON.parseObject((String) object);
        if (response == null) {
            return null;
        }
        return response.getObject("data", clazz);
    }

    public static Object getResult(Object object, Class<?> clazz,
                                   String key) {
        JSONObject response = JSON.parseObject((String) object);
        if (response == null) {
            return null;
        }
        return response.getObject(key, clazz);
    }

    public static String getResultCode(Object object) {
        JSONObject response = JSON.parseObject((String) object);
        if (response == null) {
            return "405";
        }
        return response.getObject("resultCode", String.class);
    }

    public static String getResultMessage(Object object) {
        JSONObject response = JSON.parseObject((String) object);
        if (response == null) {
            return "";
        }
        return response.getObject("resultMessage", String.class);
    }
}
